interface Htypes {
  ContentType?: string;
}
const request = async (url: string, options: any, headers?: Htypes) => {
  await nextTick() //解决刷新页面useFetch无返回
  const config = useRuntimeConfig() // baseUrl
  const reqUrl = config.public.apiBase + url

  console.log('options:--', options)
  // console.log('headers:',headers)
  // const options=options||''
  return new Promise((resolve, reject) => {
    useFetch(reqUrl, {
      ...options,
      headers: {
        token: 'token',
        'Content-Type': 'application/json',
        ...headers,
      },
    })
      .then(({ data, error }) => {
        console.log(data, 'data')
        if (error.value) {
          reject(error.value)
          return
        }
        const value: any = data.value
        if (!value) {
          console.log('执行错误了')
        } else if (value.code === 200) {
          resolve(value)
        } else {
          console.log('出现了错误')
        }
      })
      .catch((err) => {
        reject(err)
      })
  })
}
export const http = {
  get: (url: string, query: any, headers?: Htypes) => {
    return request(url, { method: 'GET', query }, headers)
  },
  post: (url: string, body: any, headers?: Htypes) => {
    return request(url, { method: 'POST', body }, headers)
  },
}
